﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.Data.SqlClient;

namespace CourseRecommend
{
    public partial class RegisterForm : Form 
    {
        private string username;
        private string password;
        private string major;

        public RegisterForm()
        {
            InitializeComponent();
        }

        private void BackButton_Click(object sender, EventArgs e)
        {
            Form LoginForm = new LoginForm();
            LoginForm.Show();
            LoginForm.Activate();
            this.Close();

        }

        private void getUserCredintials()
        {
            username = RgNameTextBox.Text;
            password = RgPasswordTextBox.Text;
            major = MajorBox.Text;
            
        }

        private void RegisterMe(string PW)
        {
            string connectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Catalog.mdf;Integrated Security=True;User Instance=True";
            SqlConnection myConnection = new SqlConnection(connectionString);

            try
            {
                myConnection.Open();
               // MessageBox.Show("connected in RegisterForm");
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }

               string sqlquery;
               sqlquery = @"INSERT INTO LoginDataTable(StudentID, Password, Major) VALUES(@username, @PW, @MJ)";
                         //  UPDATE LoginDataTable SET StudentID = @username, Password = @PW, Major = @MJ  WHERE StudentID = @username AND Password = @PW IF @@ROWCOUNT=0 
                    
                SqlCommand command = new SqlCommand(sqlquery, myConnection);
                command.Parameters.Add(new SqlParameter("@username", username));
                command.Parameters.Add(new SqlParameter("@PW", PW));
                command.Parameters.Add(new SqlParameter("@MJ", major));
                try
                {
                        command.ExecuteNonQuery();
                        MessageBox.Show("You are now registered");
                        Form LoginForm = new LoginForm();
                        LoginForm.Show();
                        LoginForm.Activate();
                        this.Close();

                    }
                    catch (Exception e)
                    {
                        MessageBox.Show("Already Registered!");
                        e.ToString();
                    }
                myConnection.Close();
        }

        private void sendBasicInfo()
        {
            string connectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Catalog.mdf;Integrated Security=True;User Instance=True";
            SqlConnection myConnection = new SqlConnection(connectionString);

            try
            {
                myConnection.Open();
                // MessageBox.Show("connected in RegisterForm");
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }

            string sqlquery;
            sqlquery = @"INSERT INTO StudentClassRecords(RecordsStudentID, RecordsClassId, GradeRecieved) VALUES(@username, @RI, @GR)";
            //  UPDATE LoginDataTable SET StudentID = @username, Password = @PW, Major = @MJ  WHERE StudentID = @username AND Password = @PW IF @@ROWCOUNT=0 

            SqlCommand command = new SqlCommand(sqlquery, myConnection);
            command.Parameters.Add(new SqlParameter("@username", username));
            command.Parameters.Add(new SqlParameter("@RI", "666"));
            command.Parameters.Add(new SqlParameter("@GR", "4"));
            try
            {
                command.ExecuteNonQuery();

            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }

            myConnection.Close();
        }

        private void sendBasicInfo2()
        {
            string connectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Catalog.mdf;Integrated Security=True;User Instance=True";
            SqlConnection myConnection = new SqlConnection(connectionString);

            try
            {
                myConnection.Open();
                // MessageBox.Show("connected in RegisterForm");
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }

            string sqlquery;
            sqlquery = @"INSERT INTO StudentClassRecords(RecordsStudentID, RecordsClassId, GradeRecieved) VALUES(@username, @RI, @GR)";
            //  UPDATE LoginDataTable SET StudentID = @username, Password = @PW, Major = @MJ  WHERE StudentID = @username AND Password = @PW IF @@ROWCOUNT=0 

            SqlCommand command = new SqlCommand(sqlquery, myConnection);
            command.Parameters.Add(new SqlParameter("@username", username));
            command.Parameters.Add(new SqlParameter("@RI", "12"));
            command.Parameters.Add(new SqlParameter("@GR", "4"));
            try
            {
                command.ExecuteNonQuery();

            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }

            myConnection.Close();
        }

        public static string HashPassword(string originalPassword)
        {
            Byte[] originalBytes;
            Byte[] hashBytes;
            MD5 md5;

            md5 = new MD5CryptoServiceProvider();
            originalBytes = ASCIIEncoding.Default.GetBytes(originalPassword);
            hashBytes = md5.ComputeHash(originalBytes);

            string PwString = System.Text.ASCIIEncoding.ASCII.GetString(hashBytes);
   //         MessageBox.Show("password in hash function",PwString);
            return PwString;
            
        }


        private void RegisterButton_Click(object sender, EventArgs e)
        {
            getUserCredintials();
         //   HashPassword(password);
            sendBasicInfo();
            sendBasicInfo2();
           // SQLConnector connect = new SQLConnector();
           // connect.createConnection();
            RegisterMe(HashPassword(password));

        }

        private void RgNameTextBox_TextChanged(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

            IList<Majors> dataSource = new List<Majors>();
            dataSource.Add(new Majors() { Major = "CS Scientific"});
            dataSource.Add(new Majors() { Major = "CS Information Technology" });
            this.MajorBox.DataSource = dataSource;
            this.MajorBox.DisplayMember = "Major";
            this.MajorBox.ValueMember = "Major";
            this.MajorBox.DropDownStyle = ComboBoxStyle.DropDown;
            //MajorBox.Items.Add("CS Scientific");
            //MajorBox.Items.Add("CS Information Technology");
            //MajorBox.Items.Add(new Majors { Major= "English" });
            
        }
    }
}
